import pandas as pd
import random
import tkinter as tk
from tkinter import messagebox
from datetime import datetime
from PIL import Image, ImageTk  # 需要安装Pillow库

# 定义全局变量
rows = 13 #行数
columns = 6 # 列数
group_arr = [2, 2, 2] #列数分布
file_path = "namelist.xlsx"

# 读取 Excel 文件中的人名
def read_names(file_path):
    try:
        df = pd.read_excel(file_path)
        return df.iloc[:, 1].tolist()
    except FileNotFoundError:
        messagebox.showerror("错误", f"未找到文件 {file_path}")
        return []
    except Exception as e:
        messagebox.showerror("错误", f"读取文件 {file_path} 时出错: {str(e)}")
        return []

# 创建教室布局界面
def create_classroom_layout(rows, columns, names, group_arr):
    root = tk.Tk()
    root.title("教室座位布局")
    root.attributes('-fullscreen', True)

    # 加载卡通背景图片
    bg_color = "#f0f8ff"  # 默认浅蓝色背景
    try:
        background_image = Image.open("background.png")  # 请确保有这张图片
        background_photo = ImageTk.PhotoImage(background_image)
        background_label = tk.Label(root, image=background_photo)
        background_label.place(x=0, y=0, relwidth=1, relheight=1)
        background_label.image = background_photo
        # 这里也设置一个合适的背景色，避免使用 transparent
        bg_color = "#f0f8ff"  
    except FileNotFoundError:
        print("未找到背景图片，使用默认背景")
        root.configure(bg=bg_color)  # 浅蓝色背景

    frame = tk.Frame(root)
    frame.pack(expand=True)

    # 获取当前日期
    now = datetime.now()
    # 添加提示文字
    prompt_label = tk.Label(frame, text="密码学测验座位安排，请大家按照自己的位置就坐！\n "+now.strftime("%Y年%m月%d日"),
                            font=("Comic Sans MS", 24, "bold"), fg="#ff69b4", bg=bg_color, anchor="center")
    prompt_label.grid(row=0, column=1, columnspan=sum(group_arr) + len(group_arr) - 1, pady=10)

    # 黑板样式，使用更卡通的颜色和字体
    blackboard = tk.Label(frame, text="黑板", font=("Comic Sans MS", 24, "bold"), bg="#008000", fg="white",
                          width=sum(group_arr) + len(group_arr) - 1, relief=tk.RAISED)
    blackboard.grid(row=1, column=1, columnspan=sum(group_arr) + len(group_arr) - 1, pady=10)

    col_index = 1
    # 显示列号，设置底色为浅绿，使用卡通字体
    for group_index, group_size in enumerate(group_arr):
        for col in range(group_size):
            col_label = tk.Label(frame, text=f"列 {col_index}", font=("Comic Sans MS", 14), bg="#90EE90", relief=tk.RAISED,
                                 anchor="center", width=15)
            col_label.grid(row=2, column=col_index, padx=2, pady=2)
            col_index += 1
        if group_index < len(group_arr) - 1:
            # 显示过道列号，设置宽度为 7，底色为灰色
            aisle_label = tk.Label(frame, text="过道", font=("Comic Sans MS", 14), bg="lightgray", relief=tk.RAISED,
                                   anchor="center", width=7)
            aisle_label.grid(row=2, column=col_index, padx=2, pady=2)
            col_index += 1

    # 显示行号，设置底色为浅绿，使用卡通字体
    for row in range(3, rows + 3):
        row_label = tk.Label(frame, text=f"行 {row - 2}", font=("Comic Sans MS", 14), bg="#90EE90", relief=tk.RAISED,
                             anchor="center", width=10)
        row_label.grid(row=row, column=0, padx=2, pady=2)

    all_seats = []
    col_index = 1
    for group_index, group_size in enumerate(group_arr):
        for col in range(group_size):
            for row in range(3, rows + 3):
                all_seats.append((row, col_index))
            col_index += 1
        if group_index < len(group_arr) - 1:
            col_index += 1  # 跳过过道列

    random.shuffle(all_seats)

    if len(names) > len(all_seats):
        messagebox.showwarning("警告", "座位数少于人数，请增加座位！")
    else:
        group_index = 0
        col_index = 1
        col_group_map = {}
        for group_size in group_arr:
            for _ in range(group_size):
                col_group_map[col_index] = group_index
                col_index += 1
            group_index += 1
            if group_index < len(group_arr):
                col_index += 1  # 跳过过道列

        name_index = 0
        for seat in all_seats:
            row, col = seat
            group_index = col_group_map[col]
            seat_color = ["#FF6347", "#32CD32", "#FFFF00", "#8A2BE2", "#1E90FF"][group_index % 5]
            if name_index < len(names):
                seat_label = tk.Label(frame, text=names[name_index], font=("Comic Sans MS", 14), bg=seat_color,
                                      relief=tk.RAISED, anchor="center", width=15)
                seat_label.grid(row=row, column=col, padx=2, pady=2)
                name_index += 1
            else:
                # 若人名用完，显示空座位，并将底色变为浅红
                seat_label = tk.Label(frame, text="空座位", font=("Comic Sans MS", 14), bg="#ffe4e1", relief=tk.RAISED,
                                      anchor="center", width=15)
                seat_label.grid(row=row, column=col, padx=2, pady=2)

    # 美化退出按钮，使用卡通风格
    exit_button = tk.Button(root, text="退出", font=("Comic Sans MS", 16), fg="white", bg="#FF4500",
                            borderwidth=0, relief=tk.RAISED, command=root.destroy)
    exit_button.pack(side=tk.BOTTOM, pady=20)

    root.mainloop()



names = read_names(file_path)

if names:
    # 创建教室布局，传入分组数组
    create_classroom_layout(rows, columns, names, group_arr)
